
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-03-23
 * Time：20:54
 */


package singlinklist;

public class Test {

    public static void main(String[] args) {
        ISingleLinkList msl =new MySingleLinkedList();

        // 插入
        System.out.println("=====插入========");

        // 尾插

        msl.insertFirst(1);




        msl.insertLast(1);
        msl.insertLast(1);
        msl.insertLast(1);
        msl.insertLast(1);
        msl.insertLast(6);
        msl.insertLast(1);
        msl.insertLast(1);
        msl.insertLast(2);
        msl.insertLast(1);
        msl.insertLast(3);
        msl.insertLast(1);
        msl.insertLast(1);

        msl.display();


//        System.out.println("======删除=======");
//
//        msl.remove(3);
//        msl.removeFirst();
//        msl.removeLast();
//        msl.display();
//
//
//        System.out.println("======修改=====");
//        msl.modify(1,2);
//        msl.display();
//
//
//        System.out.println("=====查找=====");
//        System.out.println(msl.contains(2));
//        System.out.println(msl.contains(3));
//        System.out.println(msl.length());
//
//        msl.clear();

        System.out.println("=======");
//        msl.removeVal(0);
//        msl.removeVal(1);
//        msl.removeVal(2);
//        msl.removeVal(3);
//        msl.display();

        msl.removeValAll(1);

        msl.display();


        msl.insertLast(2);
        msl.insertLast(3);

        msl.insert(3,9);
        msl.insertFirst(0);
        msl.display();


        System.out.println("======删除=======");

        msl.remove(3);
        msl.removeFirst();
        msl.removeLast();
        msl.display();


        System.out.println("======修改=====");
        msl.modify(1,2);
        msl.display();


        System.out.println("=====查找=====");
        System.out.println(msl.contains(2));
        System.out.println(msl.contains(3));
        System.out.println(msl.length());

        msl.clear();



    }
}

class Test1 {

    public static MySingleLinkedList.ListNode detectCycle(MySingleLinkedList.ListNode head) {
        MySingleLinkedList.ListNode fast =head;
        MySingleLinkedList.ListNode slow = head;

        while(fast != null && fast.next != null) {
            fast =fast .next.next;
            slow =slow.next;
            if(fast == slow) {
                break;
            }
        }

        if(fast == null || fast.next == null) {
            return null;
        }

        fast = head;

        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }

        return slow;

    }
    public static void main1(String[] args) {
        MySingleLinkedList msl=new MySingleLinkedList();
        MySingleLinkedList.ListNode head1=msl.createloop();
        MySingleLinkedList.ListNode dec=detectCycle(head1);
        System.out.println(dec.val);


    }


    public static boolean chkPalindrome( MySingleLinkedList.ListNode  A) {

        if(A==null) {
            return false;
        }

        MySingleLinkedList.ListNode  dev=A;

        MySingleLinkedList.ListNode  fast =A,slow=A;

        while(fast != null && fast.next != null) {
            slow=slow.next;
            fast=fast.next.next;
        }

        MySingleLinkedList.ListNode  cur=slow.next;

        while(cur != null) {
            MySingleLinkedList.ListNode  Ncur=cur.next;
            cur.next=slow;
            slow=cur;
            cur=Ncur;
        }

        while(A != slow) {
            if(A.val != slow.val){
                return false;
            }
            if(A.next==slow) {
                return true;
            }
            A=A.next;
            slow=slow.next;
        }

        return true;
    }

    public static void main(String[] args) {
        MySingleLinkedList ls=new MySingleLinkedList();
        ls.insertLast(2);

        ls.insertLast(1);
        ls.display();
        System.out.println(chkPalindrome(ls.head));
    }

}

